package com.ruoyi.project.module.state.controller;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.module.state.domain.State;
import com.ruoyi.project.module.state.service.IStateService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.framework.web.domain.AjaxResult;

import javax.servlet.http.HttpServletRequest;

/**
 * 用户 信息操作处理
 * 
 * @author ruoyi
 * @date 2018-11-06
 */
@Controller
@RequestMapping("/module/state")
public class StateController extends BaseController
{
    private String prefix = "module/state";
	
	@Autowired
	private IStateService stateService;
	
	@RequiresPermissions("module:state:view")
	@GetMapping()
	public String state()
	{
	    return prefix + "/state";
	}
	
	/**
	 * 查询用户列表
	 */
	@RequiresPermissions("module:state:list")
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(State state)
	{
		startPage();
        List<State> list = stateService.selectStateList(state);
		return getDataTable(list);
	}
	
	/**
	 * 新增用户
	 */
	@GetMapping("/add")
	public String add()
	{
	    return prefix + "/add";
	}
	
	/**
	 * 新增保存用户
	 */
	@RequiresPermissions("module:state:add")
	@Log(title = "用户", businessType = BusinessType.INSERT)
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(HttpServletRequest request)
	{
		request.getParameter("user_id");
		request.getParameter("dept_id");
		request.getParameter("login_name");
		request.getParameter("user_name");
		request.getParameter("user_type");
		request.getParameter("email");
		request.getParameter("phonenumber");
		request.getParameter("sex");
		request.getParameter("avatar");
		request.getParameter("password");
		request.getParameter("salt");
		request.getParameter("status");
		request.getParameter("del_flag");
		request.getParameter("login_ip");
		request.getParameter("login_date");
		request.getParameter("create_by");
		request.getParameter("create_time");
		request.getParameter("update_by");
		request.getParameter("update_time");
		request.getParameter("remark");






		State state =new State();
		try {
				state.setCreateTime(new SimpleDateFormat("yyyy-MM-dd").parse(request.getParameter("bithday")));
			} catch (ParseException e) {
				e.printStackTrace();
		}

		return toAjax(stateService.insertState(state));
	}

	/**
	 * 修改用户
	 */
	@GetMapping("/edit/{userId}")
	public String edit(@PathVariable("userId") Integer userId, ModelMap mmap)
	{
		State state = stateService.selectStateById(userId);
		mmap.put("state", state);
	    return prefix + "/edit";
	}
	
	/**
	 * 修改保存用户
	 */
	@RequiresPermissions("module:state:edit")
	@Log(title = "用户", businessType = BusinessType.UPDATE)
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(State state)
	{		
		return toAjax(stateService.updateState(state));
	}
	
	/**
	 * 删除用户
	 */
	@RequiresPermissions("module:state:remove")
	@Log(title = "用户", businessType = BusinessType.DELETE)
	@PostMapping( "/remove")
	@ResponseBody
	public AjaxResult remove(String ids)
	{		
		return toAjax(stateService.deleteStateByIds(ids));
	}
	
}
